/*TKFbizIz1sQtCe1KWOr3lGnq1gA308Bi9gkGFU6e8cc=*/
import { ajax,base,viewModel } from 'nc-lightapp-front';
const { NCProgressBar } = base;
let { setGlobalStorage, getGlobalStorage } = viewModel;
const defTradeType=['card_FCT2', 'card_Z3','card_30','card_4BL0','card_FCT1','card_Z2','card_21','card_4A80'];
const defConfig={
  'FCT2':['ntotalorigmny','norigpshamount'], //收款合同
  'Z3':['ntotalorigmny','norigpshamount'],//销售合同
  '30':['ntotalorigmny','nreceivedmny'],//销售订单
  '4BL0':['gather_rent','gather_money'],//租出合同
  'FCT1':['ntotalorigmny','norigpshamount'],//付款合同
  'Z2':['ntotalorigmny','naccpaymny'],//采购合同
  '21':['ntotalorigmny','naccpaymny'],//采购订单
  '4A80':['gather_rent','gather_money']//租入合同
}
const smartCardArea={
  SmartCardWallet_supplier:'SmartCardWallet_supplier',//供应商
  SmartCardWallet_transation:'SmartCardWallet_transation',//往来情况
  SmartCardWallet_accountage:'SmartCardWallet_accountage',//应付账款到期情况//客户应收账款逾期情况
  SmartCardWallet_customer:'SmartCardWallet_customer',//客户
  SmartCardWallet_transaction:'SmartCardWallet_transaction',//往来情况
  smartCard_FCT1:'smartCard_FCT1',
  smartCard_Z2:'smartCard_Z2',
  smartCard_21:'smartCard_21',
  smartCard_4A80:'smartCard_4A80',
  smartCard_FCT2:'smartCard_FCT2',
  smartCard_Z3:'smartCard_Z3',
  smartCard_30:'smartCard_30',
  smartCard_4BL0:'smartCard_4BL0'
}
const disabledVisibleCache={
  "SmartCardItem_busiAss": {   
    "smartCard_FCT1":true,
    "smartCard_Z2":true,
    "smartCard_21":true,
    "smartCard_4A80":true,
    "smartCard_FCT2":true,
    "smartCard_Z3":true,
    "smartCard_30":true,
    "smartCard_4BL0":true
  }
};
export {
    getStoreExpandFlag,
    triggerEventListener,
    isHasLayout,
    refreshSmartCard,
    dealSmartCardConfig,
    modifierMeta,
    dealSmartCard,
    querySmartCardData,
    disabledVisibleCache,
    smartCardArea
}



function getStoreExpandFlag(){
  let self=this;
  let moduleId="arap-smart",fold_flag={};
  let appCode = self.props.getUrlParam('appcode')||self.props.getUrlParam('c')||self.props.getSearchParam('c');
  let pageCode = self.props.getUrlParam('pagecode')||self.props.getUrlParam('p')||self.props.getSearchParam('p');
  let userDataJson=getGlobalStorage('localStorage', "STOREFORINFODATA");
  let userData=userDataJson&&JSON.parse(userDataJson);
  let userId=userData&&userData.userId;
  let drag_width_localStorage = appCode + '_' + pageCode + '_' + moduleId + '_' + userId;
  let drag_expandFlag = getGlobalStorage('localStorage', 'customer_drag_width') || {};
  if (typeof drag_expandFlag === 'string') {
    drag_expandFlag = JSON.parse(drag_expandFlag)
}
  if(drag_expandFlag[drag_width_localStorage]){
    fold_flag=drag_expandFlag[drag_width_localStorage];
  }
  // let fold_flag = getGlobalStorage('localStorage', drag_width_localStorage) || {}
  if(fold_flag["expandFlag"]){
    self.horExpandFlag=fold_flag["expandFlag"];
  }
}
function triggerEventListener(type='refresh'){
  let {smartCard}=this.props;
  smartCard&&smartCard.triggerEventListener('SmartCard_AP', type);
}
function isHasLayout(param){
  let {data}=param;
  let layoutFlag=false;
  if(data.template){
    let meta = data.template;
    let layoutData=meta.layout;
    if(layoutData){
      layoutData=JSON.parse(layoutData);
      layoutData.forEach((item,index)=>{
        if(item.name=="SmartCardContainer"||item.name=="SmartCard"){
          layoutFlag=true;
        }
      })
    }
  }
  return layoutFlag;
}
function refreshSmartCard(param){
  let {props,key,moduleId, attrcode, record}=param;
  let currentKey=key;
  if(currentKey&&currentKey.includes('_v')){
    currentKey=currentKey.replace('_v','');
  }
  let cardInstanceArr=props.smartCard.getVisibleCards('SmartCard_AP','SmartCardItem_busiAss');
  let changedField=currentKey;
  cardInstanceArr.forEach((item,index)=>{
    if(item.moduleId&&item.moduleId.includes('NCEmbedGraphic')){
      let  cardInstance = props.smartCard.getCardInstance('SmartCard_AP', item.moduleId);
      if(moduleId&&moduleId=='bodys'){
        let values=record.values;
        if(values){
          values[currentKey]=values[key];
        }
        changedField='';
        cardInstance&&cardInstance.updateEmbedGraphic({ moduleId, attrcode:currentKey, record })
      }
      cardInstance&&cardInstance.setChangedField(changedField);
    }
  })
}
function dealSmartCardConfig(){
  let moduleId=this.props.moduleId;
  if(moduleId){
    let tradeType=moduleId.split('_')[1];
    if(tradeType){
      return defConfig[tradeType];
    }
  }
}
function modifierMeta(param){
    let _this=this;
    let {props, meta,cardId}=param;
    if(!cardId){
      cardId='arapSmartCard';
    }
    if(meta&&meta[cardId]&&meta[cardId].items){
        meta[cardId].moduletype='cardTable';
        meta[cardId].items.forEach((item,index)=>{
            item.showLabel=true;
            //增加自定义字段进度条
            if(item.attrcode=='rate'){
                item.showLabel=false;
                item.render=(text, record, index)=>{
                    // let norigpshamount=record.values["norigpshamount"].value;
                    // let ntotalorigmny=record.values["ntotalorigmny"].value;
                    // let processvalue=(norigpshamount/ntotalorigmny).toFixed(2)*100;//"20";
                    let processvalue="20";
                    return(
                        <NCProgressBar 
                            // size='small' 
                            strokeWidth='6px'
                            // strokeColor={processvalue === 100 ? '#4A83EF' : '#FF4A65'}
                            showInfo={true}
                            // colors={processvalue === 100 ? 'info' : 'danger'} 
                            // now={processvalue} 
                            percent={processvalue} 
                        />
                    )
                }
            }
        })
        //增加标题 副标题
    }
    return meta;
}
//与智能区小卡数据通信
async function dealSmartCard (param){
    let {cardId,url} = param;
    let {smartCard}=this.props;
    let status=this.props.getUrlParam('status');
    // if(!(status&&status=='browse')) return;
    if(!this.horExpandFlag){
      return;
    }
    //获取智能区某个卡片的示例
    let src_billtype='',src_billid=[],pk_org='';
    let syscode=this.props.form.getFormItemsValue(this.formId, 'src_syscode').value;
    let selectedData = this.props.cardTable.getCheckedRows(this.tableId);
    let allData = this.props.cardTable.getAllData(this.tableId);
    if(selectedData&&selectedData.length>0){
      selectedData.forEach((item,index)=>{
        if (item && item.data && item.data.values&&item.data.values.src_billid&&item.data.values.src_billid.value) {
          if(src_billid.indexOf(item.data.values.src_billid.value)==-1){
            src_billid.push(item.data.values.src_billid.value);
          }
          pk_org=item.data.values.pk_org.value;
          if(item&&item.data&&item.data.values&&item.data.values.src_billtype&&item.data.values.src_billtype.value){
            src_billtype=item.data.values.src_billtype.value;
          }
        }
      })
    }else if(allData&&allData.rows&&allData.rows.length>0){
      if(allData&&allData.rows){
        allData.rows.forEach((row,index)=>{
          if(row.values.src_billid&&row.values.src_billid.value){
            if(src_billid.indexOf(row.values.src_billid.value)==-1){
              src_billid.push(row.values.src_billid.value);
            }
          }
          pk_org=row.values.pk_org.value;
          if(row.values.src_billtype&&row.values.src_billtype.value){
            src_billtype=row.values.src_billtype.value;
          }
        })
      }
    }
    let templetid={};
    defTradeType.forEach((item,index)=>{
      let smartCardProps=smartCard.getCardProps('SmartCard_AP',item,'templetId');
      if(smartCardProps){
        templetid[item]=smartCardProps;
      }
    })
    let requestData={
      src_syscode:syscode||'',
      src_billid:src_billid||[],
      src_billtype:src_billtype||'',
      pk_org: pk_org||'',
      templetid:templetid
    };

    param.requestData=requestData;
    let smallCardData=await querySmartCardData.call(this,param);
    let resTradeType=[];
    if(smallCardData&&smallCardData.data){
      resTradeType=Object.keys(smallCardData.data);
    }
    let visibleReskey=[];
    defTradeType.forEach((resKey,index)=>{
        //设置页签显示隐藏
      let isVisible=false;
      if(resTradeType&&resTradeType.length>0){
        isVisible=resTradeType.includes(resKey);
      }
      if(isVisible){
        visibleReskey.push(resKey);
      }
      setSmartCardVisible.call(this,{cardId:resKey,isVisible});
    })
    setSmallCardData.call(this,cardId,visibleReskey,smallCardData);
}
function setSmallCardData(cardId,visibleReskey,smallCardData){
  let {smartCard}=this.props;
  if(visibleReskey&&visibleReskey.length>0){
    setTimeout(()=>{
      visibleReskey.forEach((visResKey)=>{
        const cardInstance = smartCard.getCardInstance('SmartCard_AP', visResKey);
        cardInstance&&cardInstance.setSmallCardData({cardId,data:smallCardData.data[visResKey],src_billtype:visResKey});
      })
    },500)
  }
}
//设置小卡片的显示隐藏
function setSmartCardVisible(param){
    let {cardId,isVisible}=param;
    let {smartCard}=this.props;
    smartCard.setCardProps('SmartCard_AP', cardId, { "visible": isVisible},true)//设置卡片夹不显示

}
//请求小卡片数据
function querySmartCardData(param){
    let {url,requestData} =param;
    return  new Promise((resolve, reject) => {  
        ajax({
            url,
            data:requestData,
            success:(res)=>{
                resolve(res);
            },
            error: function (error) {
                reject(error);
            }
        })
    })
}
/*TKFbizIz1sQtCe1KWOr3lGnq1gA308Bi9gkGFU6e8cc=*/